package com.yangxu.hospital.tools.utils;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.response.AlipaySystemOauthTokenResponse;
import com.alipay.api.request.AlipaySystemOauthTokenRequest;

import com.alipay.api.FileItem;
import java.util.Base64;
import java.util.ArrayList;
import java.util.List;

public class AlipaySystemOauthToken {

    public static void main(String[] args) throws AlipayApiException {
        // 初始化SDK
        AlipayClient alipayClient = new DefaultAlipayClient(getAlipayConfig());

        // 构造请求参数以调用接口
        AlipaySystemOauthTokenRequest request = new AlipaySystemOauthTokenRequest();

        // 设置刷新令牌
        request.setRefreshToken("201208134b203fe6c11548bcabd8da5bb087a83b");

        // 设置授权码
        request.setCode("4b203fe6c11548bcabd8da5bb087a83b");

        // 设置授权方式
        request.setGrantType("authorization_code");

        AlipaySystemOauthTokenResponse response = alipayClient.execute(request);
        System.out.println(response.getBody());

        if (response.isSuccess()) {
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
            // sdk版本是"4.38.0.ALL"及以上,可以参考下面的示例获取诊断链接
            // String diagnosisUrl = DiagnosisUtils.getDiagnosisUrl(response);
            // System.out.println(diagnosisUrl);
        }
    }

    private static AlipayConfig getAlipayConfig() {
        String privateKey  = "MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQCwxY6/qCPfgAvLoUEuSV1XXZV4iSqO5WuszcZI/pHZJl2NhSgSjYK7Ps4AF38ZKb1rZxFn5ge3VV625rcrGwR+v46hVnNF3Xo+9aHNjptotMq9mP4SXuatw43avuwX9d9PBGDvt7xm3+hXe3Uze/ObgwZ3ddTj0Wp9LhU9l1kz+FVGhIOunuAQubkOk7gnDisycGou82WmPzYYuBWnojuEDP5wn1loKxBV9HNMnv9Fid2kX2b1lhnFDnw6OplrOcspiFF/fXgSOmytgmMqU1h06NKIVRw0jycNF48EOXZwoqxFs2MQNfXD409BAwab7+/yvSEY68XH6/t20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dfHB9TB84V20XzIIlMcJTdJl/MyWyuyFX8sUqcEUGs7dYBeMAXw0NrFlkxwyK/j4rL6jfsTd9jf3shdIjd42MtGMs8yFYEZZN3xYIKJSViyZ4yhe3QG3jw9cJw4l1Nfqn4EybwlaxgCXGBOO14Ab9uOs7VHMt2eHcgaA==";
        String alipayPublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAsMWOv6gj34ALy6FBLkldV12VeIkqjuVrrM3GSP6R2SZdjYUoEo2Cuz7OABd/GSm9a2cRZ+YHt1Vetua3KxsEfr+OoVZzRd16PvWhzY6baLTKvZj+El7mrcON2r7sF/XfTwRg77e8Zt/oV3t1M3vzm4MGd3XU49FqfS4VPZdZM/hVRoSDrp7gELm5DpO4Jw4rMnBqLvNlpj82GLgVp6I7hAz+cJ9ZaCsQVfRzTJ7/RYndpF9m9ZYZxQ58OjqZaznLKYhRf314EjpsrYJjKlNYdOjSiFUcNI8nDRePBDl2cKKsRbNjEDX1w+NPQQMGm+/v8r0hGOvFx+v7dtBdyV317QIDAQAB";
        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setServerUrl("https://openapi.alipay.com/gateway.do");
        alipayConfig.setAppId("2021004198638403");
        alipayConfig.setPrivateKey(privateKey);
        alipayConfig.setFormat("json");
        alipayConfig.setAlipayPublicKey(alipayPublicKey);
        alipayConfig.setCharset("UTF-8");
        alipayConfig.setSignType("RSA2");
        return alipayConfig;
    }
}